home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 242 / Issue 242 - April 2008 - DPCS0408DVD.ISO / Software Money Savers / VirtualDub / Source / VirtualDub-1.7.7-src.7z / src / system / source / registry.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2007-01-13  |  6.4 KB  |  230 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    System library component
  3. //    Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
  4. //
  5. //    Beginning with 1.6.0, the VirtualDub system library is licensed
  6. //    differently than the remainder of VirtualDub.  This particular file is
  7. //    thus licensed as follows (the "zlib" license):
  8. //
  9. //    This software is provided 'as-is', without any express or implied
  10. //    warranty.  In no event will the authors be held liable for any
  11. //    damages arising from the use of this software.
  12. //
  13. //    Permission is granted to anyone to use this software for any purpose,
  14. //    including commercial applications, and to alter it and redistribute it
  15. //    freely, subject to the following restrictions:
  16. //
  17. //    1.    The origin of this software must not be misrepresented; you must
  18. //        not claim that you wrote the original software. If you use this
  19. //        software in a product, an acknowledgment in the product
  20. //        documentation would be appreciated but is not required.
  21. //    2.    Altered source versions must be plainly marked as such, and must
  22. //        not be misrepresented as being the original software.
  23. //    3.    This notice may not be removed or altered from any source
  24. //        distribution.
  25.  
  26. #include "stdafx.h"
  27. #include <windows.h>
  28.  
  29. #include <vd2/system/VDString.h>
  30. #include <vd2/system/registry.h>
  31.  
  32. VDRegistryKey::VDRegistryKey(const char *pszKey, bool bGlobal) {
  33.     if (RegCreateKeyEx(bGlobal ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, (PHKEY)&pHandle, NULL))
  34.         pHandle = NULL;
  35. }
  36.  
  37. VDRegistryKey::~VDRegistryKey() {
  38.     if (pHandle)
  39.         RegCloseKey((HKEY)pHandle);
  40. }
  41.  
  42. bool VDRegistryKey::setBool(const char *pszName, bool v) const {
  43.     if (pHandle) {
  44.         DWORD dw = v;
  45.  
  46.         if (RegSetValueEx((HKEY)pHandle, pszName, 0, REG_DWORD, (const BYTE *)&dw, sizeof dw))
  47.             return true;
  48.     }
  49.  
  50.     return false;
  51. }
  52.  
  53. bool VDRegistryKey::setInt(const char *pszName, int i) const {
  54.     if (pHandle) {
  55.         DWORD dw = i;
  56.  
  57.         if (RegSetValueEx((HKEY)pHandle, pszName, 0, REG_DWORD, (const BYTE *)&dw, sizeof dw))
  58.             return true;
  59.     }
  60.  
  61.     return false;
  62. }
  63.  
  64. bool VDRegistryKey::setString(const char *pszName, const char *pszString) const {
  65.     if (pHandle) {
  66.         if (RegSetValueEx((HKEY)pHandle, pszName, 0, REG_SZ, (const BYTE *)pszString, strlen(pszString)))
  67.             return true;
  68.     }
  69.  
  70.     return false;
  71. }
  72.  
  73. bool VDRegistryKey::setString(const char *pszName, const wchar_t *pszString) const {
  74.     if (pHandle) {
  75.         if (GetVersion() & 0x80000000) {
  76.             VDStringA s(VDTextWToA(pszString));
  77.  
  78.             if (RegSetValueEx((HKEY)pHandle, pszName, 0, REG_SZ, (const BYTE *)s.data(), s.size()))
  79.                 return true;
  80.         } else {
  81.             if (RegSetValueExW((HKEY)pHandle, VDTextAToW(pszName).c_str(), 0, REG_SZ, (const BYTE *)pszString, sizeof(wchar_t) * wcslen(pszString)))
  82.                 return true;
  83.         }
  84.     }
  85.  
  86.     return false;
  87. }
  88.  
  89. bool VDRegistryKey::setBinary(const char *pszName, const char *data, int len) const {
  90.     if (pHandle) {
  91.         if (RegSetValueEx((HKEY)pHandle, pszName, 0, REG_BINARY, (const BYTE *)data, len))
  92.             return true;
  93.     }
  94.  
  95.     return false;
  96. }
  97.  
  98. bool VDRegistryKey::getBool(const char *pszName, bool def) const {
  99.     DWORD type, v, s=sizeof(DWORD);
  100.  
  101.     if (!pHandle || RegQueryValueEx((HKEY)pHandle, pszName, 0, &type, (BYTE *)&v, &s)
  102.         || type != REG_DWORD)
  103.         return def;
  104.  
  105.     return v != 0;
  106. }
  107.  
  108. int VDRegistryKey::getInt(const char *pszName, int def) const {
  109.     DWORD type, v, s=sizeof(DWORD);
  110.  
  111.     if (!pHandle || RegQueryValueEx((HKEY)pHandle, pszName, 0, &type, (BYTE *)&v, &s)
  112.         || type != REG_DWORD)
  113.         return def;
  114.  
  115.     return (int)v;
  116. }
  117.  
  118. int VDRegistryKey::getEnumInt(const char *pszName, int maxVal, int def) const {
  119.     int v = getInt(pszName, def);
  120.  
  121.     if (v<0 || v>=maxVal)
  122.         v = def;
  123.  
  124.     return v;
  125. }
  126.  
  127. bool VDRegistryKey::getString(const char *pszName, VDStringA& str) const {
  128.     DWORD type, s = sizeof(DWORD);
  129.  
  130.     if (!pHandle || RegQueryValueEx((HKEY)pHandle, pszName, 0, &type, NULL, &s) || type != REG_SZ)
  131.         return false;
  132.  
  133.     str.resize(s);
  134.     if (RegQueryValueEx((HKEY)pHandle, pszName, 0, NULL, (BYTE *)str.data(), &s))
  135.         return false;
  136.  
  137.     str.resize(strlen(str.c_str()));        // Trim off pesky terminating NULLs.
  138.  
  139.     return true;
  140. }
  141.  
  142. bool VDRegistryKey::getString(const char *pszName, VDStringW& str) const {
  143.     if (!pHandle)
  144.         return false;
  145.  
  146.     if (GetVersion() & 0x80000000) {
  147.         VDStringA v;
  148.         if (!getString(pszName, v))
  149.             return false;
  150.         str = VDTextAToW(v);
  151.         return true;
  152.     }
  153.  
  154.     const VDStringW wsName(VDTextAToW(pszName));
  155.     DWORD type, s = sizeof(DWORD);
  156.  
  157.     if (!pHandle || RegQueryValueExW((HKEY)pHandle, wsName.c_str(), 0, &type, NULL, &s) || type != REG_SZ)
  158.         return false;
  159.  
  160.     if (s > 0) {
  161.         str.resize((s + sizeof(wchar_t) - 1) / sizeof(wchar_t));
  162.  
  163.         if (RegQueryValueExW((HKEY)pHandle, wsName.c_str(), 0, NULL, (BYTE *)&str[0], &s))
  164.             return false;
  165.     }
  166.  
  167.     str.resize(wcslen(str.c_str()));        // Trim off pesky terminating NULLs.
  168.  
  169.     return true;
  170. }
  171.  
  172. int VDRegistryKey::getBinaryLength(const char *pszName) const {
  173.     DWORD type, s = sizeof(DWORD);
  174.  
  175.     if (!pHandle || RegQueryValueEx((HKEY)pHandle, pszName, 0, &type, NULL, &s)
  176.         || type != REG_BINARY)
  177.         return -1;
  178.  
  179.     return s;
  180. }
  181.  
  182. bool VDRegistryKey::getBinary(const char *pszName, char *buf, int maxlen) const {
  183.     DWORD type, s = maxlen;
  184.  
  185.     if (!pHandle || RegQueryValueEx((HKEY)pHandle, pszName, 0, &type, (BYTE *)buf, &s) || maxlen < (int)s || type != REG_BINARY)
  186.         return false;
  187.  
  188.     return true;
  189. }
  190.  
  191. bool VDRegistryKey::removeValue(const char *name) {
  192.     if (!pHandle || RegDeleteValue((HKEY)pHandle, name))
  193.         return false;
  194.  
  195.     return true;
  196. }
  197.  
  198. ///////////////////////////////////////////////////////////////////////////////
  199.  
  200. VDRegistryValueIterator::VDRegistryValueIterator(const VDRegistryKey& key)
  201.     : mpHandle(key.getRawHandle())
  202.     , mIndex(0)
  203. {
  204. }
  205.  
  206. const char *VDRegistryValueIterator::Next() {
  207.     DWORD len = sizeof(mName)/sizeof(mName[0]);
  208.     LONG error = RegEnumValueA((HKEY)mpHandle, mIndex, mName, &len, NULL, NULL, NULL, NULL);
  209.  
  210.     if (error)
  211.         return NULL;
  212.  
  213.     ++mIndex;
  214.     return mName;
  215. }
  216.  
  217. ///////////////////////////////////////////////////////////////////////////////
  218.  
  219. VDString VDRegistryAppKey::s_appbase;
  220.  
  221. VDRegistryAppKey::VDRegistryAppKey() : VDRegistryKey(s_appbase.c_str()) {
  222. }
  223.  
  224. VDRegistryAppKey::VDRegistryAppKey(const char *pszKey) : VDRegistryKey((s_appbase + pszKey).c_str()) {
  225. }
  226.  
  227. void VDRegistryAppKey::setDefaultKey(const char *pszAppName) {
  228.     s_appbase = pszAppName;
  229. }
  230.